Full Tweet Data

candidate_tweets %>%
  select(-id) %>%
  DT::datatable()
data(stop_words)
stop_words <- stop_words %>%
  add_row(word = c("realdonaldtrump", "rt", "https", "http", "cont", "t.co", "amp"),
          lexicon = c("SMART", "SMART", "SMART", "SMART", "SMART", "SMART", "SMART"))



candidate_tweets <- candidate_tweets %>%
  unnest_tokens(word, full_text) %>%
  anti_join(stop_words)
# candidate_tweets %>%
#   group_by(date) %>%
#   count(word)

candidate_tweets %>%
  count(word, sort = TRUE) %>%
  filter(n > 600) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(mapping = aes(x = n, y = word)) +
    geom_col() +
    labs(y = NULL)

Word Frequency over Time

candidate_tweets %>%
  group_by(date) %>%
  count(word) %>%
  filter(n > 3) %>%
ggplot( 
       mapping = aes(x = date, y = n)) +
  geom_jitter(alpha = 0.1, size = 2.5, width = 0.3, height = 0.3) +
  geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) +
  # scale_x_log10(labels = percent_format()) +
  # scale_y_log10(labels = percent_format()) +
  scale_color_gradient(limits = c(0, 0.001), 
                       low = "darkslategray4", high = "gray75") +
  theme(legend.position="none") +
  labs(y = "Word Frequency", x = "date")

Word Associations

Most Common

tweets_df <- read_csv(here("data", "trump_tweets.csv"))

candidate_bigrams <- tweets_df %>%
  unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
  separate(bigram, c("word1", "word2"), sep = " ") %>%
  filter(!word1 %in% stop_words$word,
         !word2 %in% stop_words$word)

candidate_bigrams %>%
  count(word1, word2, sort = TRUE) %>%
  filter(n > 75) %>%
  DT::datatable()

Corruption

bigram_assoc(candidate_bigrams, search1 = "corrupt", "corruption")

Rigged

bigram_assoc(candidate_bigrams, "rigged", "rig")
bigram_assoc(candidate_bigrams, "bias")
bigram_assoc(candidate_bigrams, "unfair")

Media

bigram_assoc(candidate_bigrams, "media", "news")

Courts

bigram_assoc(candidate_bigrams, "court", "justices")

Miscellaneous Populist Rhetoric

bigram_assoc(candidate_bigrams, "elite")
bigram_assoc(candidate_bigrams, "evil")
bigram_assoc(candidate_bigrams, "destroy")
bigram_assoc(candidate_bigrams, "steal")
bigram_assoc(candidate_bigrams, "hurt")
bigram_assoc(candidate_bigrams, "kill")
bigram_assoc(candidate_bigrams, "attack")
bigram_assoc(candidate_bigrams, "enemy")

Bigram Graph

a <- grid::arrow(type = "closed", length = unit(.15, "inches"))

candidate_bigrams %>%
  select(word1, word2) %>%
  count(word1, word2) %>%
  filter(n > 400) %>%
  graph_from_data_frame() %>%
  ggraph(layout = "fr") +
    geom_edge_link(aes(edge_alpha = n), show.legend = FALSE,
                   arrow = a, end_cap = circle(.07, 'inches')) +
    geom_node_point(color = "lightblue", size = 5) +
    geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
    theme_void()